﻿using System.Collections.Generic;
using System.Linq;
using Metro.Kashikoi.Common.ExceptionHandling;
using Metro.Kashikoi.Service.Common.Entity;
using Metro.Kashikoi.Service.Item.Entity;
using Metro.Kashikoi.ServiceContract.Item;

namespace Metro.Kashikoi.Service.Item.Business
{
    class VendorFacade : IVendorFacade
    {
        /// <summary>
        /// Creates the vendor.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="vendor">The vendor.</param>
        public void CreateVendor(IUnitOfWork context, Vendor vendor)
        {
            var rep = new Repository<Vendor>(context);

            CheckDuplicate(rep, vendor);
            vendor.Deleted = false;
            rep.Add(vendor);
        }

        /// <summary>
        /// Updates the vendor.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="vendor">The vendor.</param>
        public void UpdateVendor(IUnitOfWork context, Vendor vendor)
        {
            var rep = new Repository<Vendor>(context);

            var entity = rep.GetVendor(vendor.Id);
            if (vendor.Name != entity.Name)
            {
                CheckDuplicate(rep, vendor);
                entity.Name = vendor.Name;
            }
        }

        /// <summary>
        /// Deletes the vendor.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="vendorId">The vendor id.</param>
        public void DeleteVendor(IUnitOfWork context, int vendorId)
        {
            var rep = new Repository<Vendor>(context);

            var entity = rep.GetVendor(vendorId);
            entity.Deleted = true;
        }

        /// <summary>
        /// Gets all vendors.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>
        /// A list of all the vendor.
        /// </returns>
        public IList<Vendor> GetAllVendors(IUnitOfWork context)
        {
            var rep = new Repository<Vendor>(context);

            return rep.Where(o => o.Deleted == false).ToList();
        }

        /// <summary>
        /// Gets the vendor.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="id">The id.</param>
        /// <returns>
        /// The vendor.
        /// </returns>
        public Vendor GetVendor(IUnitOfWork context, int id)
        {
            var rep = new Repository<Vendor>(context);

            return rep.GetVendor(id);
        }

        #region Private Methods

        private void CheckDuplicate(Repository<Vendor> rep, Vendor entity)
        {
            if (rep.Count(o => o.Name == entity.Name && o.Deleted == false) > 0)
            {
                throw new AppException(string.Format(
                    "Duplicate vendor {0} found.", entity.Name));
            }
        }

        #endregion
    }
}
